﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Xml;
using IntraVision.Xml;

namespace IntraVision.Web
{
    public class ConfigurableController : ControllerBase
    {
        protected string _ConfiguratorPath;
        protected XmlDocument _ConfiguratorDocument;
        protected XmlElement _Configurator;
        protected StringDictionary _Settings;

        protected StringCollection _Styles;
        protected StringCollection _Scripts;

        public XmlElement Configurator
        {
            get { return _Configurator; }
            set { _Configurator = value; }
        }

        public ConfigurableController()
            : base()
        {
            _ConfiguratorDocument = new XmlDocument();
            _Settings = new StringDictionary();
            _Styles = new StringCollection();
            _Scripts = new StringCollection();
        }

        protected void LoadConfigurator()
        {
            string configuratorsFolder = ConfigurationManager.AppSettings["ConfiguratorsFolder"];
            if (string.IsNullOrEmpty(configuratorsFolder))
                configuratorsFolder = "~/Configurators";
            string path = Context.Server.MapPath(System.IO.Path.Combine(configuratorsFolder, _ConfiguratorPath + ".xml"));
            if (!System.IO.File.Exists(path)) return;

            _ConfiguratorDocument.Load(path);
            _Configurator = _ConfiguratorDocument.SelectSingleNode("configurator") as XmlElement;

            LoadStyles();
        }

        protected void LoadStyles()
        {
            if (_Configurator == null) return;
            foreach (XmlElement include in _Configurator.SelectNodes("include"))
            {
                string type = include.AttributeValue("type");
                if (string.IsNullOrEmpty(type)) continue;
                if (type == "css") _Styles.Add(include.InnerText.Trim());
                if (type == "script") _Scripts.Add(include.InnerText.Trim());
            }
        }

        protected void InitializeSettings()
        {
            if (_Configurator == null) return;
            foreach (XmlElement set in _Configurator.SelectNodes("settings/add"))
            {
                string key = set.AttributeValue("key");
                if (string.IsNullOrEmpty(key)) continue;

                if (!string.IsNullOrEmpty(Request[key])) _Settings[key] = Request[key];
                else _Settings[key] = set.InnerText;
            }
        }

        public override void GenerateXml()
        {
            base.GenerateXml();

            foreach (string style in _Styles)
                ResultXml.AppendElement("include", style).AppendAttribute("type", "css");
            foreach (string script in _Scripts)
                ResultXml.AppendElement("include", script).AppendAttribute("type", "js");
        }
    }
}
